/*
 * JNILogger.cpp  0.4.0 / Nov 14, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "JNILogger.h"

#include "JNIWrapper.h"
#include "JVMWrapper.h"

#include "com_cute4j_logging_Logger.h"

// JNILogger :: Public Static Member Functions. --------------------------------

/*!
 * \brief Logs information message.
 *
 * \param message the message
 * \param fileName the file name
 * \param lineNumber the line number
 */
void JNILogger::logInfo(const char *message, const char *fileName,
                        int lineNumber)
{
  log(com_cute4j_logging_Logger_LEVEL_INFO, message, fileName, lineNumber);
}

/*!
 * \brief Logs information message.
 *
 * \param message the message
 */
void JNILogger::logInfo(const char *message)
{
  log(com_cute4j_logging_Logger_LEVEL_INFO, message, 0, 0);
}

/*!
 * \brief Logs warning message.
 *
 * \param message the message
 * \param fileName the file name
 * \param lineNumber the line number
 */
void JNILogger::logWarning(const char *message, const char *fileName,
                           int lineNumber)
{
  log(com_cute4j_logging_Logger_LEVEL_WARN, message, fileName, lineNumber);
}

/*!
 * \brief Logs warning message.
 *
 * \param message the message
 */
void JNILogger::logWarning(const char *message)
{
  log(com_cute4j_logging_Logger_LEVEL_WARN, message, 0, 0);
}

/*!
 * \brief Logs error message.
 *
 * \param message the message
 * \param fileName the file name
 * \param lineNumber the line number
 */
void JNILogger::logError(const char *message, const char *fileName,
                         int lineNumber)
{
  log(com_cute4j_logging_Logger_LEVEL_ERROR, message, fileName,
      lineNumber);
}

/*!
 * \brief Logs error message.
 *
 * \param message the message
 */
void JNILogger::logError(const char *message)
{
  log(com_cute4j_logging_Logger_LEVEL_ERROR, message, 0, 0);
}

/*!
 * \brief Initializes JNILogger class.
 *
 * \param env the JNIEnv pointer
 * \param cls the Cute4j Logger class
 *
 * \exception FatalError if failed
 */
void JNILogger::initialize(JNIEnv *env, jclass cls)
{
  Q_ASSERT(env);
  Q_ASSERT(cls);
  Q_ASSERT(!sm_loggerClass);
  sm_loggerClass = (jclass)env->NewGlobalRef(cls);
  sm_logMID = env->GetStaticMethodID(cls, "log",
                                   "(ILjava/lang/String;ILjava/lang/String;)V");
  if (!sm_loggerClass || !sm_logMID) {
    env->FatalError("JNILogger initialization failed");  // does not return
  }
}

// JNILogger :: Private Static Member Function. --------------------------------

void JNILogger::log(int level, const char *message, const char *fileName,
                    int lineNumber)
{
  JNIEnv *env = JVMWrapper::getEnv();
  if (env) {
    jstring jfileName = 0;
    if (fileName) {
      jfileName = JNIWrapper::newStringUTF(env, fileName);
    }
    jstring jmessage = 0;
    if (message) {
      jmessage = JNIWrapper::newStringUTF(env, message);
    }
    JNIWrapper::callStaticVoidMethod(env, sm_loggerClass, sm_logMID, level,
                                     jfileName, lineNumber, jmessage);
  } else {
    if (level == com_cute4j_logging_Logger_LEVEL_WARN) {
      fprintf(stderr, "[Cute4j][WARN] %s\n", message);
    } else {
      fprintf(stderr, "[Cute4j][ERROR] %s\n", message);
    }
  }
}

// JNILogger :: Private Static Data Members. -----------------------------------

jclass JNILogger::sm_loggerClass = 0;
jmethodID JNILogger::sm_logMID = 0;
